function fn1() {
  async function urlPromise(url) {
    return new Promise((resolve, reject) =>
      setTimeout(() => {
        resolve(`${url} page`);
      }, 2000)
    );
  }

  async function fetchData(urls = []) {
    const proArr = urls
      .filter((url) => url.endsWith("taobao.com"))

      // url数组变形为【获取url页面内容的Promise】数组
      .map((url) => urlPromise(url));

    // 将一堆Promise调度起来
    return Promise.allSettled(proArr);
  }

  fetchData([
    "https://www.taobao.com",
    "https://www.baidu.com",
    "https://web.taobao.com",
  ]).then(
    (values) => console.log(values) //[https://www.taobao.com的内容,https://web.taobao.com的内容]
  );
}
// fn1()

/* 带有超时功能的Promise */
function fn2() {
  async function execWithTimeout(fn, ms) {
    return new Promise((resolve, reject) => {
      // 时间一到就毁约
      let timer = setTimeout(() => {
        reject("timeout");
      }, ms);

      // 网络返回数据时履约
      fn().then((page) => {
        clearTimeout(timer);
        resolve(page);
      });
    });
  }

  /*  */
  function rq() {
    return new Promise((resolve) => setTimeout(resolve, 1000, `淘宝的数据`));
  }

  function fn(url) {
    return rq("https://www.taobao.com");
  }

  execWithTimeout(fn, 3000)
    .then((value) => console.log("value=", value))
    .catch((err) => console.log("err=", err));
}
fn2();
